//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-05-03  Jed Young  Creation
//

using chunmapModel
using chunmapData

**
** VectorLayer
**
@Js
class VectorLayer : Layer
{
  DataSource dataSource { private set }

  new makeFeatureSet(FeatureSet fc)
  {
    dataSource = fc
    name = fc.metadata.name
    dim := fc.metadata.type.dim
    switch (dim)
    {
    case 0:
      renderer = Renderer.makeDef([SimplePointSym{}])
    case 1:
      renderer = Renderer.makeDef([SimpleLineSym{}])
    case 2:
      renderer = Renderer.makeDef([SimplePolygonSym{}])
    default:
      renderer = Renderer.makeDef([GeneralSym{}])
    }
  }

  new make(|This| f) { f(this) }

  static VectorLayer makeUri(Uri uri)
  {
    VectorLayer.makeFeatureSet(FeatureSet.fromStr(uri.toStr))
  }

  protected override Void renderLayer(Int i, RenderEnv r)
  {
    cond := Condition
    {
      it.envelope = r.view.getBufferEnvelope
    }

    dataSource.each(cond) |f|
    {
      r.data = f
      renderer.draw(r, i)
    }
  }

  override Envelope envelope() { dataSource.envelope }
}